/* 
 * Copyright 2014 ShangDao.Ltd  All rights reserved.
 * SiChuan ShangDao.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @MyTaskTopView.java  2014年9月22日 下午9:41:38 - Guzhu
 * @author Guzhu
 * @email:muyi126@163.com
 * @version 1.0
 */

package com.android.tiango.baba.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.android.scsd.tiango.R;
import com.android.tiango.baba.util.DensityUtil;
import com.android.tiango.baba.view.TaskScrollView.ScrollTopListener;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.view.ViewHelper;

public class MyTaskTopView extends LinearLayout implements ScrollTopListener {
	private Context mContext;
	public static boolean isShow = true;
	private int mHight;
	private ImageView mIv_down;
	private RelativeLayout mRl_1;
	private Rect normal = new Rect();
	private Rect normalScroll = new Rect();
	private TaskScrollView mTaskScrollView;
	private int nowType = TaskScrollView.DOWN;
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			Object obj = msg.obj;
			System.out.println("isShow:"+isShow+" II:"+(Boolean)obj+"  Now:"+(nowType == msg.what));
			if (nowType == msg.what) {
				return;
			} else {
				nowType = msg.what;
			}
//			Object obj = msg.obj;
			boolean ii = (Boolean)obj;
			switch (msg.what) {
			case TaskScrollView.UP:
				if (ii && !scaleUp_activity.isRunning() && !scaleDown_activity.isRunning()) {
					scaleUp_activity.start();
					if (null != scaleUp_scroll) {
						scaleUp_scroll.start();
						mTaskScrollView.layout(normalScroll.left,
								mHight + DensityUtil.dip2px(mContext, 50), normalScroll.right,
								normalScroll.bottom + mHight - DensityUtil.dip2px(mContext, 50));
					}
				}
				break;
			case TaskScrollView.DOWN:
				if (!ii && !scaleDown_activity.isRunning() && !scaleUp_activity.isRunning()) {
					scaleDown_activity.start();
					if (null != scaleDown_scroll) {
						scaleDown_scroll.start();
					}
				}
				break;

			default:
				break;
			}
		}
	};
	private LinearLayout mThisView;
	private AnimatorSet scaleUp_activity;
	private AnimatorSet scaleDown_activity;
	private AnimatorSet scaleUp_scroll;
	private AnimatorSet scaleDown_scroll;

	public TaskScrollView getmTaskScrollView() {
		return mTaskScrollView;
	}

	public void setmTaskScrollView(TaskScrollView mTaskScrollView) {
		if (normalScroll.isEmpty()) {
			// 保存正常的布局位置
			normalScroll.set(mTaskScrollView.getLeft(), mTaskScrollView.getTop(),
					mTaskScrollView.getRight(), mTaskScrollView.getBottom());
		}
		scaleUp_scroll = buildScaleUpAnimation(mTaskScrollView, 0,
				-mHight + DensityUtil.dip2px(mContext, 50), true);
		scaleDown_scroll = buildScaleUpAnimation(mTaskScrollView,
				-mHight + DensityUtil.dip2px(mContext, 50), 0, true);
		this.mTaskScrollView = mTaskScrollView;
	}

	@SuppressLint("NewApi")
	public MyTaskTopView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView(context);
	}

	public MyTaskTopView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	public MyTaskTopView(Context context) {
		super(context);
		initView(context);
	}

	private void initView(Context context) {
		mContext = context;
		mThisView = (LinearLayout) LayoutInflater.from(mContext).inflate(
				R.layout.layout_tasktopview, null);
		mThisView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT));
		mIv_down = (ImageView) mThisView.findViewById(R.id.iv_down);
		mRl_1 = (RelativeLayout) mThisView.findViewById(R.id.rl_1);
		addView(mThisView);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		mHight = mRl_1.getHeight();
		if (normal.isEmpty()) {
			// 保存正常的布局位置
			normal.set(mRl_1.getLeft(), mRl_1.getTop(), mRl_1.getRight(), mRl_1.getBottom());
			buildAnimationSet();

		}
	}

	private void buildAnimationSet() {
		scaleUp_activity = buildScaleUpAnimation(mRl_1, normal.top, -mHight, false);
		scaleDown_activity = buildScaleUpAnimation(mRl_1, -mHight, normal.top, true);
		scaleUp_activity.addListener(animationListener);
		scaleDown_activity.addListener(animationListener);
	}

	private Animator.AnimatorListener animationListener = new Animator.AnimatorListener() {
		@Override
		public void onAnimationStart(Animator animation) {
		}

		@Override
		public void onAnimationEnd(Animator animation) {
			if (isShow) {
				mRl_1.layout(normal.left, 0, normal.right, mHight);
				// mTaskScrollView.layout(normalScroll.left, DensityUtil.dip2px(mContext, 50), normalScroll.right, normalScroll.bottom);
				isShow = false;
			} else {
				System.out.println("l:" + normal.left + " t:" + (normal.top) + " r:" + normal.right
						+ " b:" + (normal.bottom));
				mRl_1.layout(normal.left, normal.top, normal.right, normal.bottom);
				mTaskScrollView.layout(normalScroll.left, normalScroll.top, normalScroll.right,
						normalScroll.bottom);
				isShow = true;
			}

		}

		@Override
		public void onAnimationCancel(Animator animation) {

		}

		@Override
		public void onAnimationRepeat(Animator animation) {

		}
	};

	private AnimatorSet buildScaleUpAnimation(View target, float targetScaleY,
			float toTargetScaleY, boolean isShow) {
		AnimatorSet scaleUp = new AnimatorSet();
		ObjectAnimator apah;
		if (isShow) {
			apah = ObjectAnimator.ofFloat(target, "alpha", 0.0f, 1.0f);
		} else {
			apah = ObjectAnimator.ofFloat(target, "alpha", 1.0f, 0f);
		}
		scaleUp.playTogether(
				ObjectAnimator.ofFloat(target, "translationY", targetScaleY, toTargetScaleY), apah);
		scaleUp.setDuration(250);
		return scaleUp;
	}

	private void showDown(View menuItem, int menu_index) {
		ViewHelper.setAlpha(menuItem, 0);
		AnimatorSet scaleUp = new AnimatorSet();
		scaleUp.playTogether(ObjectAnimator.ofFloat(menuItem, "translationY", -100.f, 0.0f),
				ObjectAnimator.ofFloat(menuItem, "alpha", 0.0f, 1.0f));
		scaleUp.setInterpolator(AnimationUtils.loadInterpolator(mContext,
				android.R.anim.anticipate_overshoot_interpolator));
		scaleUp.setStartDelay(50 * menu_index);
		scaleUp.setDuration(400).start();
	}

	// private void showDown(View menuItem, int menu_index) {
	// ViewHelper.setAlpha(menuItem, 0);
	// AnimatorSet scaleUp = new AnimatorSet();
	// scaleUp.playTogether(ObjectAnimator.ofFloat(menuItem, "translationX", -100.f, 0.0f),
	// ObjectAnimator.ofFloat(menuItem, "alpha", 0.0f, 1.0f));
	// scaleUp.setInterpolator(AnimationUtils.loadInterpolator(mContext,
	// android.R.anim.anticipate_overshoot_interpolator));
	// scaleUp.setStartDelay(50 * menu_index);
	// scaleUp.setDuration(400).start();
	// }

	@Override
	public void onScrollChanged(final boolean isUp) {
		if (isUp||isShow) {
			Message msg = mHandler.obtainMessage();
			msg.what = TaskScrollView.UP;
			msg.obj = isUp;
			mHandler.sendMessage(msg);
		} else {
			Message msg = mHandler.obtainMessage();
			msg.obj = isUp;
			msg.what = TaskScrollView.DOWN;
			mHandler.sendMessage(msg);
		}

	}

}
